package com.gitee.wsl.android.ext.okhttp.request

import androidx.core.net.toFile
import com.gitee.wsl.android.ext.okhttp.response.FileResponseMapper
import com.gitee.wsl.jvm.okhttp.response.mapper
import com.gitee.wsl.jvm.okhttp.request.asyncCall
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.File



fun Request.asyncDownLoadInCache(client: OkHttpClient, respFun:(Result<File>)->Unit) = asyncCall(client){ result ->
    result.onSuccess {
        result.getOrNull()?.mapper(FileResponseMapper())?.let { respFun( Result.success(it.toFile())) }?:respFun(
            Result.failure(RuntimeException("download false")))
    }
    result.onFailure {
        respFun(Result.failure(it))
    }
}
